package com.gxa.saas.service.impl;

import com.gxa.saas.mapper.BrandMapper;
import com.gxa.saas.mapper.BrandSecondMapper;
import com.gxa.saas.pojo.BrandFirst;
import com.gxa.saas.pojo.BrandSecond;
import com.gxa.saas.service.BrandSecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author CZJ
 * @date 2022/6/8 15:28
 */
@Service
@Transactional(rollbackFor = {Throwable.class})

public class BrandSecondServiceImpl implements BrandSecondService {

    @Autowired
    BrandSecondMapper brandSecondMapper;

    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<BrandSecond> selectByPage(Integer limit, Integer size) {
        return brandSecondMapper.selectByPage((limit-1)*size,size);
    }

    @Override
    public Integer insert(BrandSecond brandSecond) {
        BrandFirst brandFirst = brandMapper.selectById(brandSecond.getFrom());
        brandMapper.updateCount(brandFirst.getCount() + 1, brandFirst.getId());
        return brandSecondMapper.insert(brandSecond);
    }


    @Override
    public Integer update(BrandSecond brandSecond) {
        BrandSecond brandSecond1 = brandSecondMapper.selectById(brandSecond.getId());
        brandSecond.setFrom(brandSecond1.getFrom());
        return brandSecondMapper.update(brandSecond);
    }

    @Override
    public Integer delete(Integer id) {
        BrandSecond brandSecond = brandSecondMapper.selectById(id);
        BrandFirst brandFirst = brandMapper.selectById(brandSecond.getFrom());
        brandMapper.updateCount(brandFirst.getCount() - 1, brandFirst.getId());
        return brandSecondMapper.delete(id);
    }

    @Override
    public List<BrandSecond> selectByFrom(Integer from) {
        return brandSecondMapper.selectByFrom(from);
    }

    @Override
    public List<BrandSecond> selectAll() {
        return brandSecondMapper.selectAll();
    }
}
